<?php

namespace App\Repositories;

use App\Models\Area;
use App\Models\Product;
use App\Exceptions\InvalidException;
class AreaRepo
{
    public function getAll($where=[])
    {
        return Area::where(function($query)use($where){

        })->get();
    }

    public function getAreaList($where=[],$pageSize=15)
    {
        return Area::where(function($query)use($where){

        })->paginate($pageSize);
    }

    public function getAreaProductList($where=[])
    {
    	 return Area::where(function($query)use($where){

        })->with(['products'=>function($query){
        	$query->select('id','name','image','area_id','desc');
        }])->get();
    }


    public function setProductArea($product_id,$area_id)
    {
        $product = Product::where('id',$product_id)->first();

        if(!$product) {
            throw new InvalidException("产品不存在");
        }
        $product->area_id = $area_id;

        $product->save();
    }
}